
#include <stdlib.h>
#include "SecurityTokenFile.h"

/** Constructor for the file-based security token.
 * 
 * @param ifilename The file containing the key to use.
 * 
 * @param ipassword The password to unlock the key in the file. */
SecurityTokenFile::SecurityTokenFile(const char *ifilename, const char *ipassword)
{
   tried = 0;
   filename = (ifilename) ? strdup(ifilename) : NULL;
   password = (ipassword) ? strdup(ipassword) : NULL;
}

/** Destructor for the file-based security token. */
SecurityTokenFile::~SecurityTokenFile()
{
   if (filename) free(filename);
   filename = NULL;
   if (password) free(password);
   password = NULL;
}

/** SSH session authentication call.
 * 
 * @param tsession The SSH session to authenticate.
 * 
 * @param username The username to use with the file for SSH
 * authentication.
 * 
 * @return Zero on success, negative on failure. */
int SecurityTokenFile::authenticateSSHSession(LIBSSH2_SESSION *tsession, const char *username)
{
   void **abstract_pointer = libssh2_session_abstract(tsession);
   *abstract_pointer = (void *)this;
   return libssh2_userauth_publickey_fromfile(tsession,
      username, NULL, filename, password);
}
